$(function () {

    var href = window.location.href;
    if (href.indexOf('/study/index') != -1) {
        $('#open-history-list').removeClass('history-lecture-list');
    }

    loadOpenCourse(1, new Date());

    $('body').on('click', 'ul.lecture-date-num li', function () {
        if (!$(this).hasClass('active')) {
            $(this).addClass('active').siblings().removeClass('active');
            var date = new Date($(this).data('date'));
            loadOpenCourse(1, date);
        }
    }).on('click', 'div[data-open] , span[data-open]', function () {
        var openId = $(this).data('open');
        isLogin(function () {
            $.ajax({
                url: baseloaction + '/mobile/open/ajax/play',
                data: {'openId': openId},
                success: function (result) {
                    if (result.success==true) {
                        if(result.status=="REPLAY"){//回放
                            if(result.playStatus=='STATUS_URL') {//URL回放方式
                                window.location = result.playerUrl;
                            }
                            //修复iOS直播间无法登录
                            if($.device.ios) {
                                if(result.entity.playType=="CC"){
                                    _ccFixFn()
                                }
                            }
                            if(result.playStatus=='STATUS_CODE') {//播放码回放方式
                                window.location = baseloaction + '/mobile/uc/open/playBackCode?code='+result.entity.backCode+"&playType="+result.entity.playType;
                            }
                        }
                        if(result.status=='LIVE') {//直播
                            window.location = result.playerUrl;
                            if($.device.ios) {
                                if(result.entity.playType=="CC"){
                                    _ccFixFn()
                                }
                            }
                        }
                    } else {
                        $.toast(result.message);
                    }
                },
                error: function () {
                    $.toast('系统繁忙，请稍候再试');
                }
            });
        });
    }).on('click','.bespeak-btn',function () {
        var openId = $(this).data('id');
        bespeak(openId);
    });
});

/**
 * 预约免费直播
 * @param openId 免费直播ID
 */
function bespeak(openId) {
    isLogin(function () {
        $.ajax({
            url:baseloaction +'/mobile/uc/open/bespeakOpen',
            type:'post',
            dataType:'json',
            data:{
                'openId':openId
            },
            success:function (result) {
                $('span[data-id="'+openId+'"]').text('已预约').removeClass('bespeak-btn');
                $.toast(result.message);
            },
            error:function (error) {
                $.toast('系统错误，免费直播预约失败');
            }
        });
    });
}

// 加载免费直播
function loadOpenCourse(pageNo, date) {
    var params = {"year":date.getFullYear(),"month":date.getMonth() + 1, "date":date.getDate()};
    params = JSON.stringify(params);
    loadMobilePathData({
        url: baseloaction + '/mobile/open/ajax/load/history',
        contextId: 'open-history-list',
        pageNo: pageNo,
        pageCall: loadOpenCourse,
        params: params,
        callback: function (pageNo) {
        }
    });
}

//修复iOS进入直播间无法登录
function _ccFixFn() {
    var cookieName = '_csslcloud_fix';
    var cookieValue = '';
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(cookieName + '=');
        if (c_start != -1) {
            c_start = c_start + cookieName.length + 1;
            var c_end = document.cookie.indexOf(';', c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            cookieValue = document.cookie.substring(c_start, c_end);
        }
    }

    if (!cookieValue) {
        cookieValue = '_fix';
        document.cookie = cookieName + '=' + cookieValue + ';';
        window.location.replace('//view.csslcloud.net/html/_fix_.html?u=' + encodeURIComponent(window.location.href));
    }
}